package com.tongda.core.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.tongda.core.common.Constants;

/**
 * 
 * @author andy
 * 
 */
public class ServerIPScanner {

	public ArrayList<String> getLanIPList() {
		ArrayList<String> resultIPList = null;
		try {
			InitLoader initSystem = new InitLoader(
					NetWorkUtil.getLocalHostByNetworkCard());
			Thread thread = new Thread(initSystem);
			thread.start();
			thread.join();
			resultIPList = initSystem.getAvailableIPList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// if(resultIPList==null){
		// resultIPList = new ArrayList<String>();
		// }
		// resultIPList.add("192.168.2.16");
		// resultIPList.add("192.168.2.202");
		// resultIPList.add("192.168.2.203");
		return resultIPList;
	}

	private class InitLoader implements Runnable {
		private int firstIP = 2;
		private int lastIP = 255;
		private volatile ArrayList<Thread> threadQueueList;
		private final int MAXTHREADS = 30;
		private int threadNumNow;// 褰撳墠姝ｅ湪杩涜鐨勫瓙绾跨▼鏁伴噺

		private volatile ArrayList<String> toScanIPList;

		private volatile ArrayList<String> availableIPList;

		private InitLoader(String ip) {
			toScanIPList = new ArrayList<String>();
			availableIPList = new ArrayList<String>();
			threadQueueList = new ArrayList<Thread>();
			setIPAddressList(ip);
		}

		private InitLoader() throws UnknownHostException {
			this(InetAddress.getLocalHost().getHostAddress());
		}

		private synchronized ArrayList<String> getAvailableIPList() {
			try {
				while (toScanIPList.size() > 0) {
					Thread.sleep(5);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return availableIPList;
		}

		private void setIPAddressList(String ip) {
			int lastPointIndex = ip.lastIndexOf('.');
			String ipPrefix = ip.substring(0, ++lastPointIndex);
			String ipString = null;
			for (int i = firstIP; i <= lastIP; i++) {
				ipString = ipPrefix + i;
				toScanIPList.add(ipString);
			}
			System.out.println("鎵弿鎬讳釜鏁帮細" + toScanIPList.size());
		}

		public void run() {
			synchronized (this) {
				try {
					System.out.println("threadQueueList.size()"
							+ threadQueueList.size());
					while (toScanIPList.size() > 0) {
						while (threadNumNow >= MAXTHREADS) {
							for (Thread thread : threadQueueList) {
								if (!thread.getState().equals(
										Thread.State.TERMINATED)) {
									thread.join(100);
								}
								--threadNumNow;
							}
							threadQueueList = new ArrayList<Thread>();
						}
						Thread thread = new Thread(new ServerValidateRunner(
								toScanIPList.remove(0)));
						thread.start();
						threadNumNow++;
						threadQueueList.add(thread);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		private class ServerValidateRunner implements Runnable {
			private String ip;

			private ServerValidateRunner(String ip) {
				this.ip = ip;
			}

			private boolean isServerOk(String ip) {
				synchronized (this) {
					String res = CommunicationUtil
							.getByServerIpScanner("http://" + ip + ":"
									+ Constants.SERVER_PORT
									+ Constants.SERVER_CONTEXT_PATH
									+ "/mobile/index/isServerOk");
					if ("canyin-mobileserver".equals(res)) {
						return true;
					}
					return false;
				}
			}

			public void run() {
				synchronized (this) {
					if (isServerOk(ip)) {
						availableIPList.add(ip);
					}
				}
			}
		}
	}

	/*
	 * 蹇�鍒ゆ柇褰撳墠榛樿鏈嶅姟鍣ㄦ槸鍚﹀彲鐢�
	 */
	public static boolean isServerOk(String ip) {
		String res = CommunicationUtil.get("http://" + ip + ":"
				+ Constants.SERVER_PORT + Constants.SERVER_CONTEXT_PATH
				+ "/mobile/index/isServerOk");
		if ("canyin-mobileserver".equals(res)) {
			return true;
		}
		return false;
	}

}